package cn.touna.jss.modules.third.pacra.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/14.
 */
public class CallDetailsDto {
    private String callTime = "";
    private String dureation = "";
    private String callModel = "";
    private String contact = "";
    private String callAddr = "";
    private String contactAddr = "";
    private String calledType = "";

    /**
     * 通话开始时间，支持的时间格式 2017-02-12 12:30:30
     * 和 2017/02/12 12:30:30，若时间格式有误， 会处理为空
     *
     * @return
     */
    public String getCallTime() {
        return callTime;
    }

    public void setCallTime(String callTime) {
        this.callTime = callTime;
    }

    /**
     * 通话时长，单位为秒，若该值有误(比如时长为浮点数)，
     * 会处理为 0
     *
     * @return
     */
    public String getDureation() {
        return dureation;
    }


    public void setDureation(String dureation) {
        this.dureation = dureation;
    }

    /**
     * 呼叫类型，主叫、被叫
     */
    public String getCallModel() {
        return callModel;
    }

    public void setCallModel(String callModel) {
        this.callModel = callModel;
    }

    /**
     * 联系人号码， 不能为空，若号码为空， 则该条通话记
     * 录被丢弃
     *
     * @return
     */
    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    /**
     * 本机通话时所在地
     *
     * @return
     */
    public String getCallAddr() {
        return callAddr;
    }

    public void setCallAddr(String callAddr) {
        this.callAddr = callAddr;
    }

    /**
     * 联系人归属地
     *
     * @return
     */
    public String getContactAddr() {
        return contactAddr;
    }

    public void setContactAddr(String contactAddr) {
        this.contactAddr = contactAddr;
    }

    /**
     * 通话类型，国内通话、国际通话、 本地通话、 …
     *
     * @return
     */
    public String getCalledType() {
        return calledType;
    }

    public void setCalledType(String calledType) {
        this.calledType = calledType;
    }

    /**
     * 转成JSONArray字符串
     *
     * @param list
     * @return
     */
    public static String toJSONString(List<CallDetailsDto> list) {
        JSONArray arr = new JSONArray();
        for (CallDetailsDto item : list) {
            JSONObject obj = new JSONObject();
            obj.put("call_time", item.getCallTime());
            obj.put("duration", item.getDureation());
            obj.put("call_model", item.getCallModel());
            obj.put("contact", item.getContact());
            obj.put("contact_addr", item.getContactAddr());
            obj.put("called_type", item.getCalledType());
            arr.add(obj);
        }
        return JSON.toJSONString(arr);
    }

    /**
     * 转成CSV字符串
     *
     * @param list
     * @return
     */
    public static String toCsvString(List<CallDetailsDto> list) {
        StringBuilder sb = new StringBuilder();
        for (CallDetailsDto item : list) {
            sb.append(item.getCallTime() + ","
                    + item.getDureation() + ","
                    + item.getCallModel() + ","
                    + item.getContact() + ","
                    + item.getCallAddr() + ","
                    + item.getCalledType() + "\r\n");
        }
        return sb.toString();
    }

    public static List<CallDetailsDto> fromCsvString(String value) {
        String[] rows = value.split("\r\n|\r");
        List<CallDetailsDto> list = new ArrayList<>();
        for (String rowStr : rows) {
            String[] row = rowStr.split(",");
            CallDetailsDto dto = new CallDetailsDto();
            dto.setCallTime(row[0]);
            dto.setDureation(row[1]);
            dto.setCallModel(row[2]);
            dto.setContact(row[3]);
            dto.setCallAddr(row[4]);
            dto.setContactAddr(row[5]);
            dto.setCalledType(row[6]);
            list.add(dto);
        }
        return list;
    }

}
